package org.common.utils.fasttest.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

/**
 * object对象和json字符串互转
 * @author duxiang
 * @date 2023-08-21
 */
public final class JacksonUtils {


    private static final ObjectMapper mapper = new ObjectMapper();

    private JacksonUtils() {
    }

    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException var2) {
            throw new RuntimeException(obj.getClass().getName(), var2);
        }
    }


    public static <T>T toObject(String json, Class<T> cls) {
        try {
            return mapper.readValue(json, cls);
        } catch (IOException var3) {
            throw new RuntimeException(cls.getName(), var3);
        }
    }


    public static byte[] toJsonBytes(Object obj) {
        try {
            return mapper.writeValueAsBytes(obj);
        } catch (JsonProcessingException var2) {
            throw new RuntimeException(obj.getClass().getName(), var2);
        }
    }

    public static <T> T toObject(byte[] json, Class<T> cls) {
        try {
            return mapper.readValue(json, cls);
        } catch (Exception var3) {
            throw new RuntimeException(cls.getName(), var3);
        }
    }

    public static <T> T toObject(byte[] json, Type cls) {
        try {
            return mapper.readValue(json, mapper.constructType(cls));
        } catch (Exception var3) {
            throw new RuntimeException(cls.getTypeName(), var3);
        }
    }

    public static <T> T toObject(InputStream inputStream, Class<T> cls) {
        try {
            return mapper.readValue(inputStream, cls);
        } catch (IOException var3) {
            throw new RuntimeException(cls.getName(), var3);
        }
    }

    public static <T> T toObject(byte[] json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) {
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }



    public static <T> T toObject(String json, Type type) {
        try {
            return mapper.readValue(json, mapper.constructType(type));
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static <T> T toObject(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) {
        try {
            return mapper.readValue(json, typeReference);
        } catch (IOException var3) {
            throw new RuntimeException(typeReference.getClass().getName(), var3);
        }
    }

    public static <T> T toObject(InputStream inputStream, Type type) {
        try {
            return mapper.readValue(inputStream, mapper.constructType(type));
        } catch (IOException var3) {
            throw new RuntimeException(type.getTypeName(), var3);
        }
    }

    public static JsonNode toObject(String json) {
        try {
            return mapper.readTree(json);
        } catch (IOException var2) {
            throw new RuntimeException(var2);
        }
    }

    public static void registerSubtype(Class<?> clz, String type) {
        mapper.registerSubtypes(new NamedType[]{new NamedType(clz, type)});
    }

    public static ObjectNode createEmptyJsonNode() {
        return new ObjectNode(mapper.getNodeFactory());
    }

    public static ArrayNode createEmptyArrayNode() {
        return new ArrayNode(mapper.getNodeFactory());
    }

    public static JsonNode transferToJsonNode(Object obj) {
        return mapper.valueToTree(obj);
    }

    public static JavaType constructJavaType(Type type) {
        return mapper.constructType(type);
    }


    static {
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.registerModule(new EclipseCollectionsModule());
    }


}
